home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 11 / Info-Mac_XI_Disc_1.cdr_ / Info-Mac XI Disc 1.cdr / Programs / Science & Math / MacEspresso 1.0 / espresso / getopt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-12-10  |  1.1 KB  |  45 lines  |  [TEXT/R*ch]

  1. #include "port.h"
  2. /*  File   : getopt.c
  3.     Author : Henry Spencer, University of Toronto
  4.     Updated: 28 April 1984
  5.     Purpose: get option letter from argv.
  6. */
  7. #define    NullS    ((char *) 0)
  8. int getopt(int argc, char *argv[], char *optstring);
  9. char    *optarg;    /* Global argument pointer. */
  10. int    optind = 0;    /* Global argv index. */
  11.  
  12. int getopt(int argc, char *argv[], char *optstring)
  13.     {
  14.     register int c;
  15.     register char *place;
  16.     static char *scan = NullS;    /* Private scan pointer. */
  17.  
  18.     optarg = NullS;
  19.  
  20.     if (scan == NullS || *scan == '\0') {
  21.         if (optind == 0) optind++;
  22.         if (optind >= argc) return EOF;
  23.         place = argv[optind];
  24.         if (place[0] != '-' || place[1] == '\0') return EOF;
  25.         optind++;
  26.         if (place[1] == '-' && place[2] == '\0') return EOF;
  27.         scan = place+1;
  28.     }
  29.  
  30.     c = *scan++;
  31.     place = strchr(optstring, c);
  32.     if (place == NullS || c == ':') {
  33.         fprintf(stderr, "%s: unknown option %c\n", argv[0], c);
  34.         return '?';
  35.     }
  36.     if (*++place == ':') {
  37.         if (*scan != '\0') {
  38.         optarg = scan, scan = NullS;
  39.         } else {
  40.         optarg = argv[optind], optind++;
  41.         }
  42.     }
  43.     return c;
  44.     }
  45.